//
//  AXFWebServeManager.m
//  AIXF
//
//  Created by zhanghui on 2017/7/9.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFWebServeManager.h"
#import <AFNetworking.h>

@implementation AXFWebServeManager{
    
    //sessionMangager
    AFHTTPSessionManager *_manager;
}


IMPLEMENTATION_SINGLETON(AXFWebServeManager);

#pragma mark - 初始化方法
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        //配置baseURL
        NSURL *baseURL = [NSURL URLWithString:@"http://iosapi.itcast.cn/loveBeen/"];
        
        //配置baseURL
        _manager = [[AFHTTPSessionManager alloc]initWithBaseURL:baseURL];
        
        //增加支持的文件类型
     _manager.responseSerializer.acceptableContentTypes = [_manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html", nil]];
        
        //设置请求的类型响应类型
        _manager.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        
    }
    return self;
}


//配置baseURL
- (void)requestWithSussBlock:(void (^)(id))sussBLock andFailBlock:(void (^)(NSError *))faliBlock{
    
    //封装参数
    NSDictionary *parms = @{@"call":self.call};
    
  
    //发送网络请求
    [_manager POST:self.url parameters:parms progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //取出code码，判断是否是等于0 || 200，如果是的话，就直接吧data数据传递出去
        NSInteger code = [responseObject[@"code"]integerValue];
        
        
        //取出data
        if (code == 0 || code == 200) {
            //执行成功的回调
            if (sussBLock)  sussBLock(responseObject[@"data"]);
            
    
        }else{
            
            if (faliBlock) faliBlock(responseObject[@"msg"]);
                
        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (faliBlock) faliBlock(error);
    }];
    
    
    
}


@end
